<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="dao.DBdao,entity.ProductDB"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Product</title>
</head>
<body>

<script type="text/javascript" language="javaScript">
function add(){
	var  obj=document.getElementById("ID");
	for (i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
		if (obj[i].selected== true ) {
   			var text=obj[i].value;//获取当前选择项的 值 .
		}
	}
	var price = document.getElementById('price').value;
	var count = document.getElementById('count').value;
	if(confirm("确定要加入购物车?")){
		window.location.href = "cart.jsp?count="+count+"&price="+price+"&name="+text;
	}
}
</script>

<script type="text/javascript">
	function fun()
	{
		var  obj=document.getElementById("ID");
		for (i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
			if (obj[i].selected== true ) {
       			var text=obj[i].value;//获取当前选择项的 值 .
			}
		}
		var price = document.getElementById('price').value;
		var count = document.getElementById('count').value;
		var total = (count*price).toFixed(2);
		
		if(confirm("确定购买"+text+"支付"+total+"元吗?")){
			
			window.location.href = "productList.jsp";
		}
	}
</script>
	
<table border="1">
	<tr>
		<th>ID</th>
		<th>NAME</th>
		<th>PRICE</th>
		<th>INVENTORY</th>
	</tr>
	<%
	DBdao dao=new DBdao();
	List<ProductDB>list=dao.readFirstProduct();
	for(ProductDB e:list){ %>
	<tr>
	<td><%=e.getId() %></td>
	<td><%=e.getName() %></td>
	<td><%=e.getPrice() %></td>
	<td><%=new Random().nextInt(100) %></td>
	
	</tr>
	<%} %>
</table>
	<form id="product" method="post" action="productList.jsp">
    <select id="ID" name="NAME">
    <%for(ProductDB e:list){ %>
   	<option value=<%=e.getName() %>><%=e.getName() %></option>
   	<%} %>
   	</select>
   	数量：<input id='count' type="text"/>
   	单价：<input id='price' type='text'>
   	<%String productname=request.getParameter("NAME"); %>
   	<input type="submit" value="购买" onclick="fun()"/>
   	<input type="button" value="添加" onclick="add()"/>
   
   	</form>
	<input type="button" value="查看购物车" onclick="window.location.href='hello.jsp'"/>
</body>
</html>